#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution {
    public:
        int search(int A[], int n, int target) {
            int lo = 0, hi = n-1;
            while (lo < hi) {
//                for (int i = lo; i <= hi; i++)
  //                  cout << A[i] << " ";
    //            cout << endl;
                int mid = lo + (hi-lo)/2;    
                if (A[mid] == target) return mid;
                if (A[lo] <= A[mid]) {  /*left hand side is sorted*/
                    if (A[mid] >= target && target >= A[lo]) hi = mid;
                    else lo = mid+1; 
                }else if( A[mid] < A[hi]) {
                    if (A[mid] <= target && target <= A[hi]) lo = mid+1;
                    else hi = mid; 
                } 
            }
            if (A[lo] == target) return lo;
            else return -1;                         
        }
};


//test in 1337 online judge and pass all the test
int main(int argc, char **argv)
{
    Solution mySolution;

    cout << "-----------------Test 1--------------------" << endl;
    int input[] = {4, 5, 6, 7, 0, 1, 2};
    int result = mySolution.search(input, sizeof(input)/sizeof(int), 0);
    cout << "Find at Index: " << result << endl;

    cout << "-----------------Test 2--------------------" << endl;
    result = mySolution.search(input, sizeof(input)/sizeof(int), 8);
    cout << "Find at Index: " << result << endl;


    cout << "-----------------Test 3--------------------" << endl;
    result = mySolution.search(input, sizeof(input)/sizeof(int), 4);
    cout << "Find at Index: " << result << endl;


    cout << "-----------------Test 4--------------------" << endl;
    result = mySolution.search(input, sizeof(input)/sizeof(int), 2);
    cout << "Find at Index: " << result << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
